﻿
var ns, toast, actionsheet, WXPhotoPicker;
var app = new Vue({
    el: '#app',
    data: {
        form: {
            ID: 0,
            RoomID: 0,//房间ID
            OwnerName: '',//住户名称
            PhoneNumber: '',//住户电话
            VisitType: '',//---通行事由id
            VisitTypeName:'',//通行事由
            ItemInformation:'',//物品信息
            VehicleInformation:'',//车辆信息
            StartTime: '',//通行时间
            ReleaseInstructions: '',//放行说明
            FullName: '',//资源位置
        },
        outTypemsg:[],
        IsRoomOpen:false,
        imglist: [],
        maxuploadcount: CONFIG.maxuploadcount,
        showCalender:false,
    },
    methods: {
        getOutType:function(){
            var that = this;
            ns.post({
                action: 'getvisittypelist',
            }, function(succeed, data, err) {
                if (succeed) {
                    that.outTypemsg=data.list2; 
                    if (that.outTypemsg.length > 0) {
                        that.doSelectRoom(that.outTypemsg[0]);
                    }                
                }
            });
        },
        do_save: function (item) {
            var that = this;
            if (that.form.RoomID<=0) {
                api.toast({
                    msg: '请选择资源位置',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (!that.form.OwnerName) {
                api.toast({
                    msg: '请填写住户名称',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (that.form.PhoneNumber == "") {
                api.toast({
                    msg: '请填写手机号码',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (!that.form.VisitTypeName) {
                api.toast({
                    msg: '请选择通行事由',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }   
            if (that.form.StartTime == "") {
                api.toast({
                    msg: '请选择放行时间',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
          
            var filelist = [];
            for (var i = 0; i < that.imglist.length; i++) {
                filelist.push(that.imglist[i].url);
            }

            var options = {};
            options.action = "savereleasestrips";
            options.RoomID = that.form.RoomID;
            options.OwnerName = that.form.OwnerName;
            options.PhoneNumber = that.form.PhoneNumber;
            options.VisitType = that.form.VisitType;
            options.ItemInformation = that.form.ItemInformation;
            options.VehicleInformation = that.form.VehicleInformation;
            options.StartTime = that.form.StartTime+' 23:59';;
            options.ReleaseInstructions = that.form.ReleaseInstructions;

            toast.loading({
                title: "提交中",
                duration: 2000
            }, function (ret) { });
            ns.post(options, function (succeed, data, err) {

                toast.hide();
                if (succeed) {     
                    api.toast({
                        msg: '提交成功',
                        duration: 2000,
                        location: 'bottom'
                    });

                    //刷新并关闭
                    setTimeout(() => {
                        api.sendEvent({
                            name: 'do_load_data',
                        });
                        api.closeWin();
                    }, 300);
                    

                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                files: {
                    file: filelist
                }
            });
        },
        do_choose_type: function () {
            var that = this;
            that.IsRoomOpen=true;
        },
        doCloseRoom:function(){
            var that=this;
            that.IsRoomOpen=false;
        },
        choose_picture: function () {
            var that = this;
            ns.confirmPer('camera', function () {
                that.choose_picture_start();
            })
        },
        choose_picture_start:function(){
            var that = this;
            if (that.imglist.length >= that.maxuploadcount) {
                api.toast({
                    msg: '最多支持' + that.maxuploadcount + '张图片',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            api.actionSheet({
                cancelTitle: '取消',
                buttons: ['拍照', '从相册选择']
            }, function(ret, err) {
                //拍照
                if(ret.buttonIndex==1){ 
                    that.upload_choose_picture();
                    return;
                }
                //从相册选择
                if(ret.buttonIndex==2){
                    that.choose_picture_album();
                    return;
                } 
                //取消 
                else{
                    return false;  
                }  
            });
        },
        upload_choose_picture:function(){
            var that = this;
            api.getPicture({
                sourceType: 'camera',
                encodingType: 'jpg',
                destinationType: 'url',
                allowEdit: false,
                saveToPhotoAlbum: true,
            }, function(ret, err) {
                if (ret&&ret.data) {
                    that.imglist.push({
                        ID: 0,
                        url: ret.data,
                        thumbPath: ret.data,
                        isupload: true
                    });
                } 
            });
        },
        choose_picture_album:function(){
            var that = this;
            WXPhotoPicker.open({
                max: that.maxuploadcount - that.imglist.length,
                type:'image',
            }, function(ret) {
                if (ret) {
                    if (ret.list.length > 0) {
                        ret.list.forEach((item) => {
                            that.imglist.push({
                                ID: 0,
                                url: item.path,
                                thumbPath: item.path,
                                isupload: true
                            });
                        });
                    }
                }
            });
        },
        remove_img: function (index,item) {
            var that = this;
            that.imglist.splice(index, 1);
        },
        do_viewimages: function (activeIndex) {
            var that = this;
            var imageUrls = [];
            for (var i = 0; i < that.imglist.length; i++) {
                imageUrls.push(that.imglist[i].url);
            }
            var imageBrowser = api.require('imageBrowser');
            imageBrowser.openImages({
                showList: false,
                activeIndex: activeIndex,
                imageUrls: imageUrls
            });
        },
        do_choose_time:function(){
            var that=this;
            that.showCalender=true;
        },
        doSelectRoom:function(item){
            var that=this;
            that.IsRoomOpen=false;
            that.form.VisitType=item.ID;
            that.form.VisitTypeName=item.Name;
        },
        do_select_project: function () {
            var that = this;
            ns.openWin('chooseproject_frm', '选择项目');
        },
        get_select_project: function (data) {
            var that = this;
            // if (data.projectid > 0) {
            //     that.form.RoomID = data.projectid;
            // } else {
            //     that.form.RoomID = data.roomid;
            // }
            // that.form.ProjectID = data.projectid;
            // that.form.ProjectName = data.name;
            that.form.RoomID = data.roomid;
            that.form.FullName = data.name;
            that.getRoomOwner();
        },
        getRoomOwner: function () {
            var that = this;
            if(!that.form.RoomID){
                return;
            }
            ns.get({ action: 'getroomrelationdata', ProjectID: that.form.RoomID }, function (succeed, data, err) {
                if (succeed) {
                    that.form.OwnerName = data.RealName;
                    that.form.PhoneNumber = data.PhoneNumber;
                }
            });
        },
        initPicker: function () {
            var _this = this;
            (function ($, doc) {
                $.init();
                $.ready(function () {
                    var btn = document.querySelector('#choosedatetime');
                    btn.addEventListener('tap', function () {
                        var _self = this;
                        if (_self.picker) {
                            _self.picker.show(function (rs) {
                                _this.form.StartTime = rs.value;
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        } else {
                            var optionsJson = this.getAttribute('data-options') || '{}';
                            var options = JSON.parse(optionsJson);
                            var id = this.getAttribute('id');
                            _self.picker = new $.DtPicker(options);
                            _self.picker.show(function (rs) {
                                var timer = setTimeout(function () {
                                    clearTimeout(timer);
                                    _this.form.StartTime = rs.value;
                                }, 300);
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        }
                    }, false);

                });
            })(mui, document);
        },
        getDate: function (dt) {
            var y = dt.getFullYear();
            var m = dt.getMonth() + 1;
            var d = dt.getDate();
            m = m < 10 ? '0' + m : m;
            var d = d < 10 ? '0' + d : d;
            return y + '-' + m + '-' + d;
        },
        onConfirm(day) {
            var that = this;
            var y = day.getFullYear();
            var m = day.getMonth() + 1;
            var d = day.getDate();
            var nowdate = y + '-' + (m >= 10 ? m : '0' + m) + '-' + (d >= 10 ? d : '0' + d);
            that.showCalender = false;
            that.form.StartTime = nowdate;
        },
    }
});

apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    toast = new auiToast();
    actionsheet = new auiActionsheet();
    WXPhotoPicker = api.require('WXPhotoPicker');

     // app.initPicker();//底部弹出选择时间   
    app.form.StartTime=app.getDate(new Date());//默认今天
    app.getOutType();
    app.get_select_project(api.pageParam);
    api.addEventListener({
        name: 'do_choose_project_complete'
    }, function (ret) {
        app.get_select_project(ret.value);
    });

   
};
